/*
 * Copyright (c) 2023 Cshoo Org. All Rights Reserved.
 */

package org.tattoo.workflow.api;

import lombok.extern.slf4j.Slf4j;
import org.cshoo.tattoo.embed.infra.entity.Response;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.tattoo.workflow.api.dto.ApproveReq;
import org.tattoo.workflow.api.dto.ProcurementItemResp;
import org.tattoo.workflow.api.dto.ProcurementOrderResp;
import org.tattoo.workflow.api.dto.ReviewReq;
import org.tattoo.workflow.api.dto.SaveProcurementItemReq;
import org.tattoo.workflow.api.dto.SaveProcurementOrderReq;
import org.tattoo.workflow.api.dto.SubmitReq;
import org.tattoo.workflow.app.ProcurementAppService;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @author 杨翼
 * @since 1.0
 */
@RequestMapping("/procurement")
@RestController
@Slf4j
public class ProcurementController {

    @Resource
    private ProcurementAppService procurementAppService;

    /**
     * 查询采购订单
     *
     * @param id 采购订单id
     * @return 采购订单
     */
    @GetMapping("/findProcurementOrder/{id}")
    public ProcurementOrderResp findProcurementOrder(@PathVariable("id") Long id) {
        return procurementAppService.findProcurementOrder(id);
    }

    /**
     * 查询采购订单产品明细
     *
     * @param procurementOrderId 采购订单id
     * @return 采购订单产品明细
     */
    @GetMapping("/queryProcurementItems/{procurementOrderId}")
    public List<ProcurementItemResp> queryProcurementItems(@PathVariable("procurementOrderId") Long procurementOrderId) {
        return procurementAppService.queryProcurementItems(procurementOrderId);
    }

    /**
     * 保存采购订单
     *
     * @param saveProcurementOrderReq 采购订单请求
     * @return 采购订单id
     */
    @PostMapping("/saveProcurementOrder")
    public Response<Long> saveProcurementOrder(@RequestBody @Valid SaveProcurementOrderReq saveProcurementOrderReq) throws Exception {
        Long id = procurementAppService.saveProcurementOrder(saveProcurementOrderReq);
        return Response.<Long>builder().data(id).build();
    }

    /**
     * 保存采购订单明细，单条保存
     * 当前端有id传入，为修改操作，否则为新增操作
     *
     * @param saveProcurementItemReq 采购明细
     */
    @PostMapping("/saveProcurementItem")
    public Response<?> saveProcurementItem(@RequestBody @Valid SaveProcurementItemReq saveProcurementItemReq) throws Exception {
        procurementAppService.saveProcurementItem(saveProcurementItemReq);
        return Response.builder().build();
    }

    /**
     * 删除采购明细
     *
     * @param id 采购明细id
     */
    @DeleteMapping("/deleteProcurementItem/{id}")
    public Response<?> deleteProcurementItem(@PathVariable("id") Long id) {
        procurementAppService.deleteProcurementItem(id);
        return Response.builder().build();
    }

    /**
     * 提交采购订单，提交时将产生对应的工作流
     *
     * @param submitReq 提交请求
     */
    @PostMapping("/submitProcurementOrder")
    public Response<?> submitProcurementOrder(@RequestBody SubmitReq submitReq) throws Exception {
        procurementAppService.submitProcurementOrder(submitReq);
        return Response.builder().build();
    }

    /**
     * 复核采购订单
     *
     * @param reviewReq 复核请求
     */
    @PostMapping("/reviewProcurementOrder")
    public Response<?> reviewProcurementOrder(@RequestBody @Valid ReviewReq reviewReq) throws Exception {
        procurementAppService.reviewProcurementOrder(reviewReq);
        return Response.builder().build();
    }

    /**
     * 审批采购订单
     *
     * @param approveReq 审批请求
     */
    @PostMapping("/approveProcurementOrder")
    public Response<?> approveProcurementOrder(@RequestBody ApproveReq approveReq) throws Exception {
        procurementAppService.approveProcurementOrder(approveReq);
        return Response.builder().build();
    }
}
